package com.chenyi.leetcode.editor.cn;

/**
 * [53]最大子数组和
 *
 * @author by chenyi
 * @className MaximumSubarray
 * @date 2022-06-21 14:47:32
 */
public class MaximumSubarray {
    public static void main(String[] args) {
        Solution solution = new MaximumSubarray().new Solution();
        System.out.println(solution.maxSubArray(new int[]{-2, -1}));
    }

    @SuppressWarnings("AlibabaCommentsMustBeJavadocFormat")
            //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int maxSubArray(int[] nums) {
//            int pre = 0, maxAns = nums[0];
//            for (int x : nums) {
//                pre = Math.max(pre + x, x);
//                maxAns = Math.max(maxAns, pre);
//            }
//            return maxAns;
            int pre = 0;
            int maxValue = 0;
            for (int i : nums) {
                pre = Math.max(pre + i, i);
                maxValue = Math.max(maxValue, pre);
            }
            return maxValue;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}
